#include "GameLaunchScene.h"
#include "PTScene.h"
#include "ThemeScene.h"
#include "GameData.h"
#include <pthread.h>
#include "SceneManager.h"
#include "CommonImage.h"
#include "MusicController.h"
#include "Utils.h"

using namespace std;

#define PRELOAD_IMAGE_COUNT 13

GameData * m_gGameData;
extern MusicController * s_gController;

int m_preloadImageCount = 0;
int m_perProgress = 0;

string GameLaunchScene::title() {
	return "Loading...";
}

CCScene* GameLaunchScene::scene() {
	CCScene *scene = CCScene::create();

	GameLaunchScene *layer = GameLaunchScene::create();

	scene->addChild(layer);
	return scene;
}

bool GameLaunchScene::init() {
	if (!CCLayer::init()) {
		return false;
	}

	setKeypadEnabled(true);

	//init data from plist
	m_gGameData = GameData::getInstance();

	CCArray * array = m_gGameData->getAllImages();
	int count = array->count();

	m_preloadImageCount = PRELOAD_IMAGE_COUNT + count;
	m_perProgress = 100 / m_preloadImageCount;

	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCLayerColor *background = CCLayerColor::create(ccc4(255, 255, 255, 255));
	addChild(background, -10);

	CCSprite * loading = CCSprite::create("scene/loading.png");
	loading->setPosition(ccp(size.width / 2, size.height / 2 + 50));
	addChild(loading, -5);

	CCSprite * progressBg = CCSprite::create("scene/progress_bg.png");
	progressBg->setPosition(ccp(size.width / 2, size.height / 2 - 100));
	addChild(progressBg);

	m_progressTimer = CCProgressTimer::create(
			CCSprite::create("scene/progress.png"));

	m_progressTimer->setType(kCCProgressTimerTypeBar);
	m_progressTimer->setMidpoint(ccp(0, 0));
	m_progressTimer->setBarChangeRate(ccp(1, 0));

	m_progressTimer->setPosition(size.width / 2, size.height / 2 - 100);
	m_progressTimer->setPercentage(60);

	addChild(m_progressTimer);

	//bg
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/game_main_bg.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));

	//button easy mode
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-easy-mode-normal.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-easy-mode-pressed.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	//button hard mode
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-hard-mode-normal.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-hard-mode-pressed.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	//button abnormal mode
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-abnormal-mode-normal.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-abnormal-mode-pressed.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	//button quit
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-quit-normal.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-quit-pressed.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	//button continue
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-continue-normal.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-continue-pressed.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	//button select theme
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-select-normal.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));
	CCTextureCache::sharedTextureCache()->addImageAsync(
			"scene/btn-select-pressed.png", this,
			callfuncO_selector(GameLaunchScene::onLoad));

	//theme
	for (int i = 0; i < count; i++) {
		CommonImage * image = (CommonImage *) (array->objectAtIndex(i));
		CCTextureCache::sharedTextureCache()->addImageAsync(
				image->getImagePath(), this,
				callfuncO_selector(GameLaunchScene::onLoad));
	}

	return true;
}

void GameLaunchScene::onEasyBegin(CCObject * pSender) {
	CCLog("onBegin");
	CCScene * scene = ThemeScene::scene(kTheme, -1);
	CCDirector::sharedDirector()->replaceScene(scene);
}

void GameLaunchScene::onHardBegin(CCObject * pSender) {

}

void GameLaunchScene::onAbnormalBegin(CCObject * pSender) {

}

void GameLaunchScene::onLoad(CCObject * pSender) {
	m_preloadImageCount--;
	CCLog("NULL,onLoad...index = %d", m_preloadImageCount);
	m_progressTimer->setPercentage(100 - m_preloadImageCount * m_perProgress);

	if (m_preloadImageCount <= 0) {
		CCSize size = CCDirector::sharedDirector()->getWinSize();
		//quit menu
		CCMenuItemImage * quitMenuItem = CCMenuItemImage::create("scene/btn-quit-normal.png",
				"scene/btn-quit-pressed.png", this,
				menu_selector(GameLaunchScene::onQuit));

		//easy menu
		CCMenuItemImage * easyMenuItem = CCMenuItemImage::create("scene/btn-easy-mode-normal.png",
				"scene/btn-easy-mode-pressed.png", this,
				menu_selector(GameLaunchScene::onEasyBegin));

		//hard menu
		CCMenuItemImage * hardMenuItem = CCMenuItemImage::create("scene/btn-hard-mode-normal.png",
				"scene/btn-hard-mode-pressed.png", this,
				menu_selector(GameLaunchScene::onHardBegin));
		//abnormal menu
		CCMenuItemImage * abnormalMenuItem = CCMenuItemImage::create("scene/btn-abnormal-mode-normal.png",
				"scene/btn-abnormal-mode-pressed.png", this,
				menu_selector(GameLaunchScene::onAbnormalBegin));

		m_pMenu = CCMenu::create(easyMenuItem,hardMenuItem,abnormalMenuItem,quitMenuItem, NULL);
		m_pMenu->alignItemsVerticallyWithPadding(20);
		m_pMenu->setPosition(size.width / 2, size.height / 2 );

		addChild(m_pMenu, 2);

		CCSprite *sprite = CCSprite::create("scene/game_main_bg.png");
		sprite->setPosition(ccp(size.width / 2, size.height / 2));
		addChild(sprite, 1);
	}

}

void GameLaunchScene::onQuit(CCObject * pSender) {
	CCLog("onQuit");
	CCDirector::sharedDirector()->end();
}

void GameLaunchScene::onEnter() {
	CCLayer::onEnter();
}

void GameLaunchScene::keyBackClicked() {
	backCallback (NULL);
}

void GameLaunchScene::backCallback(CCObject * pSender) {
	if (m_gGameData)
		m_gGameData->release();
	if (s_gController)
		s_gController->release();

	CCTextureCache::sharedTextureCache()->removeAllTextures();

	CCDirector::sharedDirector()->end();
}

